package projectx.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;

public class TicketUtil {
	enum OnlyKey {
		getInstance();

		private int num = 1000;
		private String old = "";

		private String dateTime() {
			SimpleDateFormat fmt = new SimpleDateFormat("yyMMddHHmmss");
			return fmt.format(Calendar.getInstance().getTime());
		}

		public String getKey() {
			synchronized (old) {
				String now = dateTime();
				if (num > 5000 && !now.equals(old)) {
					now = dateTime();
					num = 1000;
				}

				old = now;
				return now + num++;
			}
		}
	}

	public static String getTicket(String pre) {
		return pre + OnlyKey.getInstance.getKey() + getRandom(100);
	}

	public static String getRandom(int no) {
		int temp = (int) (Math.random() * no);
		String result = "00000" + temp;
		result = result.substring(result.length() - (int) Math.log10(100),
				result.length());
		return result;
	}

	public static void main(String args[]) {
		for (int i = 0; i < 100; i++)
			System.err.println(TicketUtil.getTicket("USR"));
	}
}
